|
|
|
|
|
|
|
Using Seek on Table Recordsets |
|
|
|
|
|
|
|
|
The fastest way to locate a specific record is to use the Seek method on a table object. The Seek method performs an indexed search for the first occurrence of the record that matches the index criteria. This search uses the type of index used by ISAM-type databases. Indexed searches are easy to perform and are very fast. |
|
|
|
|
|
|
|
|
Modify the PRJMOVE.VBP project to illustrate index searching by adding another button to the form. Set the button's Name property to cmdSeek and its Caption property to &Seek. Next, add Listing 10.4 to the cmdSeek_Click event. |
|
|
|
|
|
|
|
|
Listing 10.4. Coding the cmdSeek_Click event. |
|
|
|
|
|
|
|
|
Private Sub cmdSeek_Click()
'
' use the seek method to locate a record
'
Dim strSeek As String
'
strSeek = InputBox("Enter an Author ID Seek Value:", "Table Seek", "10")
strSeek = Trim(strSeek)
'
If strSeek <> "" Then
rs.Seek "=", strSeek
If rs.NoMatch = True Then
MsgBox "Unable to locate [" & strSeek & "]", vbExclamation, "Table
Seek Failed"
Else
ReadRow
MsgBox "Found [" & strSeek & "]", vbInformation, "Table Seek Suc-
ceeded"
End If
End If
'
End Sub |
|
|
|
|
|
|
|
|
Listing 10.4 does three things. First, it prompts the user to enter a value for which to search. Second, the code confirms that the user entered a value and then performs the Seek operation. After performing the Seek operation, the code uses the NoMatch method to get the results of the Seek operation (this is the third operation performed in this routine). The results of the search are then posted in a message box. If the search was successful, the new record is loaded into the form controls. |
|
|
|
|
|
|
|
|
To make this routine work, you have to make a few changes to code in the Form_Load event. Change vbOpenDynaset to vbOpenTable, and then add the following line to the end of the routine, just after the OpenRecordset line: |
|
|
|
|
|
|
|
|
rs.Index = "PrimaryKey" ' set index property |
|
|
|
|
|